Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code structure #425

Merged
merged 18 commits into from
Feb 11, 2025
Merged

Code structure #425

merged 18 commits into from
Feb 11, 2025

Conversation

boocmp
Copy link
Collaborator

@boocmp boocmp commented Feb 4, 2025

The test code has been moved to the test/unit/ folder, replicating the src directory structure. In my opinion, this improves the efficiency of working with the codebase.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: d5a6a67 Previous: 7919bdd Ratio
rule-match-browserlike/brave-list 1766321560 ns/iter (± 17538388) 1745226241 ns/iter (± 10688991) 1.01
rule-match-first-request/brave-list 1018953 ns/iter (± 7037) 1003256 ns/iter (± 7610) 1.02
blocker_new/brave-list 209918857 ns/iter (± 4443194) 210108247 ns/iter (± 7007989) 1.00
memory-usage/brave-list-initial 41409969 ns/iter (± 3) 41409969 ns/iter (± 3) 1
memory-usage/brave-list-after-1000-requests 44005995 ns/iter (± 3) 44005995 ns/iter (± 3) 1

This comment was automatically generated by workflow using github-action-benchmark.

@boocmp boocmp self-assigned this Feb 4, 2025
@boocmp boocmp marked this pull request as ready for review February 4, 2025 12:51
@@ -894,13 +941,17 @@ impl NetworkFilter {
/// emulate the behavior of hosts-style blocking.
pub fn parse_hosts_style(hostname: &str, debug: bool) -> Result<Self, NetworkFilterError> {
// Make sure the hostname doesn't contain any invalid characters
static INVALID_CHARS: Lazy<Regex> = Lazy::new(|| Regex::new("[/^*!?$&(){}\\[\\]+=~`\\s|@,'\"><:;]").unwrap());
static INVALID_CHARS: Lazy<Regex> =
Lazy::new(|| Regex::new("[/^*!?$&(){}\\[\\]+=~`\\s|@,'\"><:;]").unwrap());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reported by reviewdog 🐶
[semgrep] expect or unwrap called in function returning a Result

Source: https://semgrep.dev/r/trailofbits.rs.panic-in-function-returning-result.panic-in-function-returning-result


Cc @thypon @kdenhartog

let original_rule = *filter
.raw_line
.clone()
.expect("All rules should be in debug mode");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reported by reviewdog 🐶
[semgrep] expect or unwrap called in function returning a Result

Source: https://semgrep.dev/r/trailofbits.rs.panic-in-function-returning-result.panic-in-function-returning-result


Cc @thypon @kdenhartog

let original_rule = *filter
.raw_line
.clone()
.expect("All rules should be in debug mode");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reported by reviewdog 🐶
[semgrep] expect or unwrap called in function returning a Result

Source: https://semgrep.dev/r/trailofbits.rs.panic-in-function-returning-result.panic-in-function-returning-result


Cc @thypon @kdenhartog


/*
#[cfg(test)]
mod optimization_tests_union_domain {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a duplicate? about the same block on line 190.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this PR I just moved the code and fixed the compilation. The code moved as is, but I guess I can clean up it as well if everyone don't mind.

@antonok-edm
Copy link
Collaborator

@boocmp what exactly is the justification for moving all the tests? Codebase churn and personal opinions aside, it's considered idiomatic Rust to keep the unit tests close to the corresponding implementations.

Ref:

Unit Tests

The purpose of unit tests is to test each unit of code in isolation from the rest of the code to quickly pinpoint where code is and isn’t working as expected. You’ll put unit tests in the src directory in each file with the code that they’re testing. The convention is to create a module named tests in each file to contain the test functions and to annotate the module with cfg(test).

@@ -1,77 +0,0 @@
use regex::Regex;

pub fn get_hostname_regex(url: &str) -> Option<(usize, (usize, usize))> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it a unused code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@boocmp
Copy link
Collaborator Author

boocmp commented Feb 6, 2025

@boocmp what exactly is the justification for moving all the tests? Codebase churn and personal opinions aside, it's considered idiomatic Rust to keep the unit tests close to the corresponding implementations.

The first reason is to reduce the amount of code in files. It’s difficult to work with a file that has over a thousand lines of code, especially when most of it consists of tests. For example, a task like adding code to the end of a file becomes quite tedious you have to search for where the tests begin instead of simply pressing the End key. The second reason is search. I can configure a search for any substring while ignoring tests by simply excluding the test directory. The third reason is GitHub diffs. It’s much clearer to see where changes are made in the code versus in the tests, since GitHub only shows a few lines around the changes, it’s not always obvious otherwise. I’m not an expert in Rust, but this recommendation goes against everything I’ve seen in other languages and projects throughout my experience.

@boocmp boocmp merged commit 98aa69f into master Feb 11, 2025
11 checks passed
@boocmp boocmp deleted the code_structure branch February 11, 2025 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants